package com.mibotiquin.alarmas;

import java.util.ArrayList;
import java.util.Iterator;

import com.actionbarsherlock.app.SherlockActivity;
import com.mibotiquin.R;
import com.mibotiquin.DbHelper.BaseUtil;
import com.mibotiquin.DbHelper.DatabaseItem;
import com.mibotiquin.menu.Principal;
import com.mibotiquin.misfarmacos.FragmentTabs;
import com.mibotiquin.misfarmacos.ItemFarmaco;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

public class CadNotice extends Activity {
	
	private int id;
	private Intent intent;
	private String[] data;
	private boolean show;
	private boolean remove;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		intent = getIntent();
		Bundle bundle = intent.getExtras();
		id = bundle.getInt("ID");
		
		final BaseUtil bd = BaseUtil.getInstance(this);
		String query = "SELECT * FROM cad WHERE id = "+String.valueOf(id);
		ItemCad item = new ItemCad();
		ArrayList<DatabaseItem> ar = bd.consulta(query, item);
		
		Iterator<DatabaseItem> it = ar.iterator();
		
		if (it.hasNext()) {
			
			item = (ItemCad)it.next();
			data = item.getDatosArray();
			
			show = Boolean.valueOf(data[4]);
			remove = Boolean.valueOf(data[5]);
			
			if (true) { //bundle.getBoolean("SHOW") && show
				//TODO: read from... somewhere if show or not
				
				Notification notif = new Notification(R.drawable.ic_launcher, data[2] + " va a caducar", System.currentTimeMillis());
				
				Intent intent2 = new Intent(this.getBaseContext(), FragmentTabs.class);
				Bundle bundle2 = new Bundle();
				intent2.putExtras(bundle2);
				PendingIntent pIntent = PendingIntent.getActivity(this.getBaseContext(), Integer.parseInt(data[0]), intent2, 0);
				
				notif.setLatestEventInfo(this.getBaseContext(), data[2], "Medicamento a punto de caducar", pIntent);
				
				notif.flags = Notification.FLAG_AUTO_CANCEL;
				
				NotificationManager mManager;
				mManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
				mManager.notify(Integer.parseInt(data[0]), notif);
			}
			
			if (bundle.getBoolean("REM") && remove) {

//TODO: remove the medicine
			}
		}
		
		finish();
		
	}

}
